package com.turing.usdtpay.mvp.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.turing.usdtpay.R;
import com.turing.usdtpay.base.BaseActivity;
import com.turing.usdtpay.mvp.contract.PhoneContract;
import com.turing.usdtpay.mvp.presenter.PhonePresenter;
import com.turing.usdtpay.mvp.view.BaseTitleView;
import com.turing.usdtpay.utils.FormatUtils;
import com.turing.usdtpay.utils.ToastUtils;

/**
 * @作者：JAY
 * @创建时间：2019/10/16 17:56
 * @描述：
 **/
public class PhoneActivity extends Activity implements View.OnClickListener, PhoneContract.View {

    private BaseTitleView mTitle;
    private Button mCommit, mUnbind;
    private EditText mPhone, mVerify;
    private TextView mSend, mBindNumber;
    private int time = 120;
    private boolean isCount = false;
    private boolean isBind = false;
    private PhoneContract.Present mPresent;
    private LinearLayout mBindLayout, mUnbindLayout;
    private String phone;
    private CountDownTimer countDownTimer = new CountDownTimer(120000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            isCount = true;
            time = --time;
            mSend.setText(time + "秒");
        }

        @Override
        public void onFinish() {
            time = 120;
            isCount = false;
            mSend.setText("发送");
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_phone);
        initView();
        initTitle();
        setPresenter(new PhonePresenter(this));
        initData();

    }

    private void initData() {
        Intent intent = getIntent();
        String tel = intent.getStringExtra("tel");
        if (!TextUtils.isEmpty(tel)) {
            isBind = true;
            mPhone.setHint("请输入您已绑定的手机号码");
            mBindLayout.setVisibility(View.GONE);
            mUnbindLayout.setVisibility(View.VISIBLE);
            String hidePhone = FormatUtils.hidePhoneNumber(tel);
            String s = "您已绑定了手机<font color='#ff0000'>" + hidePhone + "</font>";
            mBindNumber.setText(Html.fromHtml(s));
        } else {
            isBind = false;
            mPhone.setHint("请输入您要绑定的手机号码");
            mBindLayout.setVisibility(View.VISIBLE);
            mUnbindLayout.setVisibility(View.GONE);
        }

    }

    private void initTitle() {
        mTitle.setLeftShow(true);
        mTitle.setLeftButtonListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mTitle.setTitle("手机绑定");
    }

    private void initView() {
        mTitle = (BaseTitleView) findViewById(R.id.title);
        mCommit = (Button) findViewById(R.id.btn_commit);
        mSend = (TextView) findViewById(R.id.tv_send);
        mPhone = (EditText) findViewById(R.id.et_phone);
        mVerify = (EditText) findViewById(R.id.et_verify);
        mBindNumber = (TextView) findViewById(R.id.tv_bind_num);
        mUnbind = (Button) findViewById(R.id.btn_unbind);
        mBindLayout = (LinearLayout) findViewById(R.id.bind_layout);
        mUnbindLayout = (LinearLayout) findViewById(R.id.unbind_layout);

        mSend.setOnClickListener(this);
        mCommit.setOnClickListener(this);
        mUnbind.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == mSend.getId()) {
            if (!FormatUtils.isMobile(mPhone.getText().toString())) {
                ToastUtils.showNormolToast(this, "请输入正确格式手机号码");
                return;
            }
            if (isCount) {
                ToastUtils.showToast(this, "请稍后再试");
                return;
            }
            countDownTimer.start();
            if (!isBind) {
                mPresent.sendBindCode(this, mPhone.getText().toString());    //绑定
            } else {
                mPresent.sendUnbindCode(this, mPhone.getText().toString());  //解绑
            }

        } else if (v.getId() == mCommit.getId()) {
            if (!FormatUtils.isMobile(mPhone.getText().toString())) {
                ToastUtils.showNormolToast(this, "请输入正确格式手机号码");
                return;
            }
            if (TextUtils.isEmpty(mVerify.getText().toString())) {
                ToastUtils.showNormolToast(this, "请输入验证码");
                return;
            }
            if (!isBind) {
                mPresent.bind(this, mPhone.getText().toString(), mVerify.getText().toString());
                phone = mPhone.getText().toString();
            } else {
                mPresent.unbind(this, mPhone.getText().toString(), mVerify.getText().toString());
                phone = mPhone.getText().toString();
            }

        } else if (v.getId() == mUnbind.getId()) {
            mBindLayout.setVisibility(View.VISIBLE);
            mUnbindLayout.setVisibility(View.GONE);
            mPhone.setHint("请输入您已绑定的手机号码");
            mPhone.setText("");
            mVerify.setText("");
        }
    }

    private void stopTimer() {
        countDownTimer.cancel();
        time = 120;
        isCount = false;
        mSend.setText("发送");
    }

    @Override
    protected void onDestroy() {
        if (countDownTimer != null) {
            countDownTimer.cancel();
            countDownTimer = null;
        }
        super.onDestroy();
    }

    @Override
    public void sendSuccess() {
        ToastUtils.showNormolToast(this, "发送成功");
    }

    @Override
    public void bindSuccess() {
        isBind = true;
        ToastUtils.showNormolToast(this, "提交成功");
        Intent intent = new Intent();
        intent.putExtra("tel",phone);
        setResult(4,intent);
        finish();
    }

    @Override
    public void unbindSuccess() {
        isBind = false;
        ToastUtils.showNormolToast(this, "提交成功");
        Intent intent = new Intent();
        intent.putExtra("tel","");
        setResult(4,intent);
        finish();
    }

    @Override
    public void getSuccess(String phone) {

    }


    @Override
    public void onFail(String msg) {
        ToastUtils.showNormolToast(this, msg);
    }

    @Override
    public void setPresenter(PhoneContract.Present presenter) {
        mPresent = presenter;
    }
}
